package com.sihan.framework.etm.controller.business;

import java.util.Date;
import java.util.List;

import com.sihan.framework.etm.dto.AtlasAnswerDTO;
import com.sihan.framework.etm.dto.SignDTO;
import com.sihan.framework.etm.dto.business.AtlasPositionDTO;
import com.sihan.framework.etm.service.DeviceMarkerService;
import com.sihan.framework.etm.vo.business.LocalizerVO;
import com.sihan.framework.etm.dto.TicketLIstDTO;
import com.sihan.framework.etm.vo.business.WXCoordinateVO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.dto.baseData.DevicePostionDTO;
import com.sihan.framework.etm.dto.baseData.ToolDTO;
import com.sihan.framework.etm.dto.business.ToolPositonDTO;
import com.sihan.framework.etm.dto.business.WarehouseQueryReqDTO;
import com.sihan.framework.etm.service.business.ToolPositionService;
import com.sihan.framework.etm.vo.business.ToolPositionDetailVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "工器具定位跟踪")
@RestController
@RequestMapping("/bs/v0/position")
public class ToolPositionController {

    @Autowired
    private ToolPositionService toolPositionService;
    @Autowired
    private DeviceMarkerService deviceMarkerService;

    @ApiOperation(value = "查询单个工器具定位信息", notes = "查询单个工器具定位信息", response = ToolPositionDetailVO.class)
    @GetMapping("/find_by_id")
    @BusinessLog(value = "查询单个工器具定位信息")
    public ApiResult<?> selectToolCoordinateInfoById(String id) {
        return ApiResult.success(toolPositionService.selectToolCoordinateInfoById(id));
    }

    @ApiOperation(value = "地图查询所有定位信息", notes = "地图查询所有定位信息", response = ToolPositionDetailVO.class)
    @GetMapping("/map")
    @BusinessLog(value = "地图查询所有定位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "toolCode", value = "工器具编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "deviceCode", value = "柜子编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "typeModel", value = "类型与型号", dataType = "string", paramType = "query")
    })
    public ApiResult<?> selectToolCoordinateForMap(ToolPositonDTO dto) {
        return ApiResult.success(toolPositionService.selectToolCoordinateForMap(dto));
    }
//
//    @ApiOperation(value = "地图查询所有定位信息", notes = "地图查询所有定位信息", response = ToolPositionDetailVO.class)
//    @GetMapping("/v2.0/map")
//    @BusinessLog(value = "地图查询所有定位信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "toolType", value = "工器具编号", dataType = "string", paramType = "query"),
//            @ApiImplicitParam(name = "deviceCode", value = "柜子编号", dataType = "string", paramType = "query"),
//            @ApiImplicitParam(name = "typeModel", value = "类型与型号", dataType = "string", paramType = "query")
//    })
//    @RequiresPermissions("bs:manager:tool:position")
//    public ApiResult<?> selectToolMap(ToolPositonDTO dto) {
//        return ApiResult.success(toolPositionService.selectToolCoordinateForMap(dto));
//    }

//	/**
//	 *
//	 * @description
//	 * @param dto
//	 * @return
//	 * @auther 韩兵
//	 * @Date 2021年1月19日
//	 * @return ApiResult<?>
//	 */
//	@ApiOperation(value = "地图查询所有定位信息-数组形式", notes = "地图查询所有定位信息-数组形式", response = ToolPositionDetailVO.class)
//	@GetMapping("/array/map")
//	@BusinessLog(value = "地图查询所有定位信息-数组形式")
//	@RequiresPermissions("bs:manager:tool:position")
//	public ApiResult<?> selectArrayToolCoordinateForMap(DevicePostionDTO dto){
//		return ApiResult.success(toolPositionService.selectArrayToolCoordinateForMap(dto));
//	}

    /**
     * 2.0版本
     *
     * @return
     */
    @ApiOperation(value = "查询设备信息-地图", notes = "根据id查询定位信息-数组形式", response = TicketLIstDTO.class)
    @GetMapping("/array/v2.0/inquire")
    @BusinessLog(value = "查询设备信息-地图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备Id", dataType = "string"),
            @ApiImplicitParam(name = "name", value = "设备名称", dataType = "string"),
            @ApiImplicitParam(name = "taskCode", value = "工作票号", dataType = "string"),
            @ApiImplicitParam(name = "code", value = "设备编号", dataType = "string"),
            @ApiImplicitParam(name = "typeId", value = "设备类型", dataType = "string"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "date"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "date")
    })
    public ApiResult<List<AtlasAnswerDTO>> inquire(AtlasPositionDTO dto) {
        //如果查询条件都为空 则不查询
        if( (null != dto.getTypeId() && !"".equals(dto.getTypeId())) || (null != dto.getCode() && !"".equals(dto.getCode())) || (null != dto.getTaskCode() && !"".equals(dto.getTaskCode()))){

            return ApiResult.success(toolPositionService.selectArrayToolMap(dto));
        }
        return ApiResult.success(null);
    }



    @ApiOperation(value = "根据可视区域加载设备信息", notes = "根据id查询定位信息-数组形式", response = TicketLIstDTO.class)
    @GetMapping("/array/v2.0/map")
    @BusinessLog(value = "根据可视区域加载设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备Id", dataType = "string"),
            @ApiImplicitParam(name = "name", value = "设备名称", dataType = "string"),
            @ApiImplicitParam(name = "taskCode", value = "工作票号", dataType = "string"),
            @ApiImplicitParam(name = "code", value = "设备编号", dataType = "string"),
            @ApiImplicitParam(name = "typeId", value = "设备类型", dataType = "string"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "date"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "date"),
            @ApiImplicitParam(name = "lb", value = "左下角坐标", dataType = "string"),
            @ApiImplicitParam(name = "lt ", value = "左上角坐标", dataType = "string"),
            @ApiImplicitParam(name = "rb", value = "右下角坐标", dataType = "string"),
            @ApiImplicitParam(name = "rt", value = "右上角坐标", dataType = "string"),
    })
    public ApiResult<List<AtlasAnswerDTO>> selectArrayToolCoordinateForMap(AtlasPositionDTO dto) {
        List<AtlasAnswerDTO> objects = toolPositionService.selectArrayToolCoordinateForMap(dto);
        return  ApiResult.success(objects);
    }



    @ApiOperation(value = "微信小程序-查询定位信息", notes = "微信小程序-查询定位信息", response = TicketLIstDTO.class)
    @GetMapping("/array/wxMap")
    @BusinessLog(value = "微信小程序-查询定位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "查询", dataType = "string")
    })
    @RequiresAuthentication
    public ApiResult<List<LocalizerVO>> wxMap(String name) {

        return ApiResult.success(toolPositionService.wxMap(name));
    }

    @ApiOperation(value = "定位信息列表", notes = "定位信息列表", response = ToolPositionDetailVO.class)
    @GetMapping("/pages")
    @BusinessLog(value = "定位信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "toolCode", value = "工器具编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "deviceCode", value = "柜子编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "typeModel", value = "类型与型号", dataType = "string", paramType = "query")
    })
    @RequiresPermissions("bs:manager:tool:position")
    public ApiResult<?> selectPage(ToolPositonDTO dto) {
        return ApiResult.success(toolPositionService.selectPage(dto));
    }

    @ApiOperation(value = "出入库记录", notes = "出入库记录", response = ToolPositionDetailVO.class)
    @GetMapping("/warehouse")
    @BusinessLog(value = "出入库记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inQueryStartTime", value = "入库查询开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "inQueryEndTime", value = "入库查询结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "outQueryStartTime", value = "出库查询开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "outQueryEndTime", value = "出库查询结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "optionDeviceLog", value = "是否绑定定位装置", dataType = "string", paramType = "query")
    })
    public ApiResult<?> selectWarehouseRecord(WarehouseQueryReqDTO dto) {
        return ApiResult.success(toolPositionService.selectWarehouseRecord(dto));
    }


    @ApiOperation(value = "定位装置绑定记录", notes = "定位装置绑定记录", response = ToolPositionDetailVO.class)
    @GetMapping("/bind-record")
    @BusinessLog(value = "定位装置绑定记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inQueryStartTime", value = "入库查询开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "inQueryEndTime", value = "入库查询结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "outQueryStartTime", value = "出库查询开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "outQueryEndTime", value = "出库查询结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "optionDeviceLog", value = "是否绑定定位装置", dataType = "string", paramType = "query")
    })
    public ApiResult<?> deviceBindRecord(DevicePostionDTO dto) {
        return ApiResult.success(toolPositionService.selectDeviceBindRecord(dto));
    }

    @ApiOperation(value = "查询工器具运行轨迹", notes = "查询工器具运行轨迹", response = List.class)
    @GetMapping("/tool/track")
    @BusinessLog(value = "查询工器具运行轨迹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工器具id", dataType = "long", paramType = "query", required = true),
            @ApiImplicitParam(name = "queryStartTime", value = "查询开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "queryEndTime", value = "查询结束时间", dataType = "string", paramType = "query")
    })
    public ApiResult<?> selectToolCoordinateTrack(ToolDTO dto) {
        return ApiResult.success(toolPositionService.selectToolCoorHistory(dto));
    }

    @ApiOperation(value = "查询工器具运行轨迹-带标点", notes = "查询工器具运行轨迹-带标点", response = List.class)
    @GetMapping("/tool/trackSign")
    @BusinessLog(value = "查询工器具运行轨迹-带标点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工器具id", dataType = "long", paramType = "query", required = true),
            @ApiImplicitParam(name = "isHitch", value = "是否查询挂接点", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "queryStartTime", value = "查询开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "queryEndTime", value = "查询结束时间", dataType = "string", paramType = "query")
    })
    public ApiResult<?> trackSign(ToolDTO dto) {
        return ApiResult.success(toolPositionService.trackSign(dto));
    }

    @ApiOperation(value = "查询工器具运行轨迹-微信小程序", notes = "查询工器具运行轨迹-微信小程序", response = TicketLIstDTO.class)
    @GetMapping("/tool/getWXTrack")
    @BusinessLog(value = "查询工器具运行轨迹-微信小程序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工器具id", dataType = "string"),
            @ApiImplicitParam(name = "queryStartTime", value = "查询开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "queryEndTime", value = "查询结束时间", dataType = "string", paramType = "query")
    })
    public ApiResult<List<WXCoordinateVO>> getWXCoordinateList(ToolDTO dto) {

        return ApiResult.success(toolPositionService.getWXCoordinateList(dto));
    }

    @ApiOperation(value = "查询默认定位标点", notes = "查询默认定位标点")
    @GetMapping("/tool/getSign")
    @BusinessLog(value = "查询默认定位标点")
    public ApiResult<List<SignDTO>> getSign() {
        return ApiResult.success(deviceMarkerService.selectSign());
    }

}
